<#include "app/task/filters/filter-for-my-work-plan-list.ftl">

<table class="work-plan-list" data-page="${page}" data-page-size="${pageSize}" data-count="${count}">
	<tr class="title-row">
		<th class="text-center" width="55">操作</th>
		<th class="text-center" width="45">序号</th>
		<th class="text-center">合同编号</th>
		<th class="text-center">合同名称</th>
		<th class="text-center">工作内容</th>
		<th class="text-center">里程碑</th>
		<th class="text-center">开始时间</th>
		<th class="text-center">结束时间</th>
		<th class="text-center">责任人</th>
		<th class="text-center">计划变更</th>
		<th class="text-center">执行情况</th>
		<th class="text-center">备注</th>
	</tr>
	<#if data??> <#list data as p>
	<!--  -->
	<#assign url=((p.contract.mainContract)?string('contract','sub-contract'))!'contract'>
	<tr class="${((p.completed)?string('text-deleted',((p.overDate)?string('text-danger',''))))}" style="cursor: pointer;" data-url="${url}" data-cid="${p.contract.id}">
		<td class="text-center"><#if p.completed?? && !p.completed>
			<div class="btn btn-danger btn-xs work-by-plan" data-id="${p.contract.id}" data-plan-id="${p.id}">完成</div> </#if>
		</td>
		<td class="text-center cell-for-open">${p_index+1}</td>
		<td class="text-center cell-for-open">${p.contract.no}</td>
		<td class="text-center cell-for-open">${p.contract.name}</td>
		<td class="text-center cell-for-open">${p.work}</td>
		<td class="text-center cell-for-open">${p.milestone.value}</td>
		<td class="text-center cell-for-open">${(p.startDatePoint?string('yyyy-MM-dd'))!'-'}</td>
		<td class="text-center cell-for-open">${(p.datePoint?string('yyyy-MM-dd'))!'-'}</td>
		<td class="text-center cell-for-open">${p.header}</td>
		<td class="text-center cell-for-open">${(p.changed?string('已变更','未变更'))!'-'}</td>
		<td class="text-center cell-for-open">${((p.completed)?string('已执行','未执行'))}</td>
		<td class="text-center cell-for-open">${(p.remark?replace('\n',' <br> '))!'-'}
		</td>
	</tr>
	</#list> </#if>
</table>

<script type="text/javascript">
	require([ 'suredyList', 'suredyDatetimepicker', 'jqueryForm' ], function(List) {
		var doSearch = function(page, pageSize) {
			var form = $('form.filter-form');

			form.ajaxSubmit({
				dataType : 'html',
				data : {
					page : page,
					pageSize : pageSize
				},
				success : function(html, textStatus, jqXHR) {
					if (!html)
						return false;

					$('div.suredy-content').html(html);
				},
				error : function(jqXHR, textStatus, errorThrown) {
					return false;
				}
			});
		};

		List('.work-plan-list', {
			header : false,
			paginate : function(page, pageSize, key) {
				doSearch(page, pageSize);
			}
		});

		$('.work-plan-list').on('click', '.cell-for-open', function() {
			var $this = $(this);
			var tr = $this.parent('tr');

			window.open('${request.contextPath}/' + tr.data('url') + '/view/' + tr.data('cid'), '_blank');
		});

		// 完成工作
		$('.work-plan-list').on('click', '.btn.work-by-plan', function() {
			if (!confirm('是否确认执行此操作？')) {
				return false;
			}

			var $this = $(this);
			var id = $this.data('id'); // 合同
			var planId = $this.data('plan-id'); // 付款计划

			$.ajax({
				url : '${request.contextPath}/contract/work-completed-by-plan',
				type : 'post',
				data : {
					id : id,
					planId : planId
				},
				dataType : 'json',
				success : function(result, textStatus, jqXHR) {
					if (!result || !result.success) {
						var msg = (result && result.msg) || '操作失败！';
						alert(msg);
						return false;
					}

					Suredy.loadContent('${request.contextPath}/contract-plan/my-work-plan-list');
				},
				error : function(jqXHR, textStatus, errorThrown) {
					alert('操作失败！\n\n' + errorThrown);
				}
			});
		});

		$('form.filter-form .btn.filter-search').on('click', function() {
			doSearch(1, $('table.payout-plan-list').data('page-size'));
		});

		$('form.filter-form .btn.filter-reset').on('click', function() {
			$('form.filter-form input').val('');
			$('form.filter-form option:first-child').prop('selected', true);
		});
	});
</script>
